#include "XmlResponseMessageHandler.h"

XmlResponseMessageHandler::XmlResponseMessageHandler(void)
{
	type = Constant::ResponseMessage;
	cardCount = 0;
	lsCard = new CardInfo[MAX_CARD_IN_RESPONSE];
}

XmlResponseMessageHandler::XmlResponseMessageHandler(char* xmlString) : XmlHandler(xmlString) {
	type = Constant::ResponseMessage;
	cardCount = 0;
	lsCard = new CardInfo[MAX_CARD_IN_RESPONSE];
	TiXmlElement* subjectNode = rootNode->FirstChildElement("Subject");
	subject = (char*) subjectNode->GetText();

	TiXmlNode* cardNode = 0;
	while(cardNode = rootNode->IterateChildren("Card", cardNode)) {
		TiXmlElement* cardEle = cardNode->ToElement();
		CardInfo* card = new CardInfo();
		card->cardName = (char*) cardEle->Attribute("name");
		cardEle->Attribute("id", &card->id);

		TiXmlNode* attNode = 0;
		while(attNode = cardEle->IterateChildren("Attribute", attNode)) {
			Attribute att = parseAttribute(attNode->ToElement());
			card->addAttributes(att);
		}
		lsCard[cardCount++] = *card;
	}
}

XmlResponseMessageHandler::~XmlResponseMessageHandler(void)
{
}

void XmlResponseMessageHandler::addAttributeType() {
	rootNode->SetAttribute("type", Constant::ResponseMessage);
}

void XmlResponseMessageHandler::addBody() {
	//insert subject node
	TiXmlElement* subjectNode = new TiXmlElement("Subject");	
	subjectNode->InsertEndChild(*(new TiXmlText(subject)));
	rootNode->InsertEndChild(*subjectNode);

	//insert list card node
	for (int i = 0; i < cardCount; i++) {
		TiXmlElement* cardNode = new TiXmlElement("Card");

		//insert card name
		cardNode->SetAttribute("name", lsCard[i].cardName);

		//insert card id
		cardNode->SetAttribute("id", lsCard[i].id);

		//insert attributes
		int attCount;
		Attribute* lsAtt = lsCard[i].getAttributes(attCount);
		for (int j = 0; j < attCount; j++) {
			addAttributeNode(cardNode, lsAtt[j]);
		}
		rootNode->InsertEndChild(*cardNode);
	}
}

void XmlResponseMessageHandler::addAttributeNode(TiXmlElement* cardNode, Attribute att) {
	TiXmlElement* attNode = new TiXmlElement("Attribute");
	attNode->SetAttribute("key", att.key);
	attNode->SetAttribute("id", att.id);
	attNode->SetAttribute("isHidden", att.isHidden);

	TiXmlElement* valueNode = new TiXmlElement("Value");
	valueNode->InsertEndChild(*(new TiXmlText(att.value)));
	attNode->InsertEndChild(*valueNode);

	int metaDataCount;
	Metadata* lsMetaData = att.getMetaData(metaDataCount);
	for (int i = 0; i < metaDataCount; i++) {
		addMetaDataNode(attNode, lsMetaData[i]);
	}
	cardNode->InsertEndChild(*attNode);
}

void XmlResponseMessageHandler::addMetaDataNode(TiXmlElement* attNode, Metadata mt) {
	TiXmlElement* metaNode = new TiXmlElement("metadata");
	metaNode->SetAttribute("terminal", mt.terminal);

	TiXmlElement* appNode = new TiXmlElement("app");
	appNode->InsertEndChild(*(new TiXmlText(mt.app)));
	metaNode->InsertEndChild(*appNode);

	TiXmlElement* linkNode = new TiXmlElement("link");
	linkNode->InsertEndChild(*(new TiXmlText(mt.link)));
	metaNode->InsertEndChild(*linkNode);

	TiXmlElement* classNameNode = new TiXmlElement("classname");
	classNameNode->InsertEndChild(*(new TiXmlText(mt.className)));
	metaNode->InsertEndChild(*classNameNode);

	attNode->InsertEndChild(*metaNode);
}

Attribute XmlResponseMessageHandler::parseAttribute(TiXmlElement *attNode) {
	Attribute* att = new Attribute();
	att->key = (char*) attNode->Attribute("key");
	attNode->Attribute("id", &att->id);
	attNode->Attribute("isHidden", &att->isHidden);

	TiXmlElement* valueNode = attNode->FirstChildElement("Value");
	att->value = (char*) attNode->GetText();

	TiXmlNode* metaNode = 0;
	while (metaNode = attNode->IterateChildren("metadata", metaNode)) {
		Metadata metadata = parseMetadata(metaNode->ToElement());
	}
	return *att;
}

Metadata XmlResponseMessageHandler::parseMetadata(TiXmlElement *metaNode) {
	Metadata* mtData = new Metadata();
	mtData->terminal = (char*) metaNode->Attribute("terminal");
	mtData->app = (char*) metaNode->FirstChildElement("app")->GetText();
	mtData->className = (char*) metaNode->FirstChildElement("classname")->GetText();
	mtData->link = (char*) metaNode->FirstChildElement("link")->GetText();
	return *mtData;
}

void XmlResponseMessageHandler::setCards(CardInfo* cards, int size) {
	cardCount = size;
	lsCard = cards;
}